package com.gao.web.controller;

import com.gao.web.pojo.Customer;
import com.gao.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;

@Controller
public class UserController extends HttpServlet {
    @Autowired
    UserService userService;

    @RequestMapping("/inseruser")
    public String insert(String username, String password, String name, String sex, String phone, String email, String address, Model model) {

        int Sex = Integer.parseInt(sex);
        int i = userService.insertCustomer(username, password, name, Sex, phone, email, address);
        if (i == 1) {
            model.addAttribute("e", "1");
            return "/addCustomer.jsp";
        } else {
            model.addAttribute("e", "0");
            return "/addCustomer.jsp";
        }
    }

    @RequestMapping("/deleteuser")
    public String delete(String id, Model model) {
        int Id = Integer.parseInt(id);
        int i = userService.delete(Id);
        if (i == 1) {
            model.addAttribute("data", "0");
            return "customer_info.jsp";
        } else {
            model.addAttribute("data", "1");
            return "customer_info.jsp";
        }
    }

    @RequestMapping("/login")
    public String login(String username, String password, Model model) {
        System.out.println("接受login请求");
        Customer customer = userService.login(username, password);
        if (customer != null) {
            model.addAttribute("data", username);
            return "/index.jsp";
        } else {
            model.addAttribute("data", "用户名或密码错误");
            return "/login.jsp";
        }
    }

    @RequestMapping("/adminlogin")
    public String adminlogin(String admin, String password, Model model) {

        if (admin.equals("admin") & password.equals("123456")) {
            model.addAttribute("data", "登录成功!");
            return "/admin.jsp";
        } else {
            model.addAttribute("data", "登录失败!");
            return "/admin_login.jsp";
        }
    }

    @RequestMapping("/register")
    public String register(String username, String password, String phone, Model model) {
        System.out.println("接受register请求");
        int i = userService.register(username, password, phone);
        if (i == 1) {
            model.addAttribute("data", "注册成功!");
            return "/register.jsp";
        } else {
            model.addAttribute("data", "用户已存在！");
            return "/register.jsp";
        }
    }
}

